package com.eleMall.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.eleMall.entity.Result;
import com.sun.prism.impl.BaseContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import com.eleMall.entity.ShoppingCart;
import com.eleMall.service.ShoppingCartService;

import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 购物车(ShoppingCart)表控制层
 *
 * @author makejava
 * @since 2024-03-12 13:36:53
 */
@Slf4j
@RestController
@RequestMapping("/eleMall/shoppingCart")
@Api(tags = "购物车(ShoppingCart)表控制层")
public class ShoppingCartController {
    /**
     * 服务对象
     */
    @Resource
    private ShoppingCartService shoppingCartService;

    /**
     * 分页查询
     *
     * @param page 页码
     * @param pageSize 每页大小
     * @param id 用户id
     * @return 查询结果
     */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result page(int page,int pageSize,long id) {
        //分页构造器
        Page<ShoppingCart> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        //根据用户id进行查询
        queryWrapper.eq(ShoppingCart::getUserId,id);
        //添加排序条件，根据id进行排序
        queryWrapper.orderByAsc(ShoppingCart::getId);

        //分页查询
        shoppingCartService.page(pageInfo,queryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation("通过主键查询单条数据")
    @GetMapping("{id}")
    public Result queryById(@ApiParam(value = "id 主键") @PathVariable("id") Long id) {
        ShoppingCart shoppingCart = shoppingCartService.getById(id);
        return Result.success(shoppingCart);
    }

    /**
     * 新增数据
     *
     * @param shoppingCart 实体
     * @return 新增结果
     */
    @ApiOperation("新增数据")
    @PostMapping
    public Result add(@ApiParam(value = "shoppingCart 实体") @RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.save(shoppingCart);
        return Result.success("新增成功");
    }

    /**
     * 编辑数据
     *
     * @param shoppingCart 实体
     * @return 编辑结果
     */
    @ApiOperation("编辑数据")
    @PutMapping
    public Result edit(@ApiParam(value = "shoppingCart 实体") @RequestBody ShoppingCart shoppingCart) {
        shoppingCartService.updateById(shoppingCart);
        return Result.success("修改成功");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation("删除数据")
    @DeleteMapping
    public Result deleteById(@ApiParam(value = "id 主键") Long id) {
        shoppingCartService.removeById(id);
        return Result.success("删除成功");
    }

    /**
     * 清空购物车
     * @param userId 用户id
     * @return
     */
    @ApiOperation("清空购物车")
    @DeleteMapping("/clean")
    public Result<String> clean(@ApiParam(value = "userId 主键") Long userId){

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, userId);

        shoppingCartService.remove(queryWrapper);

        return Result.success("清空购物车成功");
    }

}


